package 算法;

public class BasicSearchDemo4 {
    public static void main(String[] args) {
        //27 22 30 40 36 13 19 16 20 7 10 43 50 48
        int arr[]={27,22,30,40,36,
                13,19,16,20,
                7,10,
                43,50,48};
        //查找的数
        int number=50;
        //分成四个块对象
        Block1 b4=new Block1(22,36,0,4);
        Block1 b5=new Block1(13,20,5,8);
        Block1 b6=new Block1(7,10,9,10);
        Block1 b7=new Block1(43,50,11,13);
        //放入数组中
        Block1 blockarr[]={b4,b5,b6,b7};
        //写方法查找
        int Indext=getIndex(blockarr,arr,number);
        System.out.println(Indext);
    }
    //查索引
    public static int getIndex(Block1 []blockarr,int arr[],int number){
        int k=find(blockarr,number);
        if(k==-1)
        {
            return -1;
        }
        else{
            for(int i=blockarr[k].getStarIndex();i<=blockarr[k].getEbdIndex();i++)
            {
                if(number==arr[i])
                {
                    return i;
                }
            }
        }
        return -1;
    }
    //查找块
    public static int find(Block1[] blockarr,int number){
        for(int i=0;i< blockarr.length;i++)
        {
            if(number<=blockarr[i].getMax()&&number>=blockarr[i].getMin())
            {
                return i;
            }
        }
        return -1;
    }
}
class Block1{
    private  int min;
    private  int max;
    private  int starIndex;
    private  int ebdIndex;

    public Block1() {
    }

    public Block1(int min, int max, int starIndex, int ebdIndex) {
        this.min = min;
        this.max = max;
        this.starIndex = starIndex;
        this.ebdIndex = ebdIndex;
    }

    /**
     * 获取
     * @return min
     */
    public int getMin() {
        return min;
    }

    /**
     * 设置
     * @param min
     */
    public void setMin(int min) {
        this.min = min;
    }

    /**
     * 获取
     * @return max
     */
    public int getMax() {
        return max;
    }

    /**
     * 设置
     * @param max
     */
    public void setMax(int max) {
        this.max = max;
    }

    /**
     * 获取
     * @return starIndex
     */
    public int getStarIndex() {
        return starIndex;
    }

    /**
     * 设置
     * @param starIndex
     */
    public void setStarIndex(int starIndex) {
        this.starIndex = starIndex;
    }

    /**
     * 获取
     * @return ebdIndex
     */
    public int getEbdIndex() {
        return ebdIndex;
    }

    /**
     * 设置
     * @param ebdIndex
     */
    public void setEbdIndex(int ebdIndex) {
        this.ebdIndex = ebdIndex;
    }

    public String toString() {
        return "Block1{min = " + min + ", max = " + max + ", starIndex = " + starIndex + ", ebdIndex = " + ebdIndex + "}";
    }
}
